<?php
/**
 * Created by PhpStorm .
 * User: {Administrator}
 * Date: {2022/7/8 16:42}
 * Author: '树叶'
 * Email: 'ayunu@qq.com'
 * Motto Info: '理想如果不向现实做一点点屈服，那么理想也将归于尘土。'
 */

namespace app\api\model;
use think\Model;
use think\Db;
use app\api\model\User as UserModel;

class Config extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [

    ];

    /**
     * 普通代理
     */
    public function getAgent()
    {
        $anent = Db::name('config')->where('group','price_manag')->where('name','price_agent')->value('value');
        $num = $anent / 100;
        return $num;
    }

    /**
     * 超级代理
     */
    public function getOneAgent()
    {
        $anent = Db::name('config')->where('group','price_manag')->where('name','price_area')->value('value');
        $num = $anent / 100;
        return $num;
    }


    /**
     * 获取协议
     * 类型1=用户协议,2=安装店,3=代理商
     */
    public function getAgreement($type)
    {
        $where = [];
        $where['group'] = ['eq','agreement'];
        $where['name'] = ['eq','user_agreement'];


        if ($type == 2)
        {
            $where['name'] = ['eq','shop_agreement'];
        }

        if ($type == 3)
        {
            $where['name'] = ['eq','agent_agreement'];
        }

        $res = Db::name('config')->where($where)->value('value');

        return $res;
    }


    /**
     * 获取安装说明
     */

    public function getInstallation($name,$group)
    {
        $res = Db::name('config')->where('name',$name)->where('group',$group)->find();
        if (!$res) return false;

        return $res;
    }

    /**
     * 积分规则
     */
    public function getIntegralrules()
    {
       return Db::name('config')->where('name','Integralrules')->where('group','integral')->value('value');
    }

    /**
     * 签到积分获取
     */
    public static function getSing()
    {
        return Db::name('config')->where('name','inter_sign')->where('group','integral')->value('value');
    }

    /**
     *  分享注册积分
     */
    public function Shareintegral()
    {
        $res = Db::name('config')->where('name','inter_invitation')->where('group','integral')->value('value');
        return $res;
    }

    public function Getuserprice($uid)
    {
        $UserModel = new UserModel();
        $res = $UserModel->getUserInfo($uid);
        $where = [];
        $where['group'] = ['eq','price_manag'];
        if ($res['is_vip'] == 2)
        {
            $where['name'] = ['eq','agent'];
            $config = Db::name('config')->where($where)->value('value');

        }elseif ($res['is_vip'] == 3)
        {
            $where['name'] = ['eq','oneagent'];
            $config = Db::name('config')->where($where)->value('value');
        }else{

            $config = [];
        }

        $data = empty($config) ? [] : json_decode($config,true);

        foreach ($data as $key => &$v)
        {
            $data[$key] = bcdiv($v, 100, 2);
        }
        return $data;
    }

    /**
     * 积分比例
     */
    public static function interPrice()
    {
        $value = Config::where('name','inter_price')->where('group','integral')->value('value');
        return $value;
    }

    /**
     * 退款信息
     */
    public static function refund()
    {
       return Config::where('group','refund')->column('value');
    }


    /**
     * 条约
     */
    public static function rules($type)
    {
        $data = Config::where('group',$type)->column('value');
        return $data;
    }
    /**
     * 获取偏远地区
     */

    public function getRemoteAreas()
    {
        $res = Db::name('config')->where('name',"remote_area")->where('group',"postage")->value("value");
        $res = json_decode($res,true);
        return $res;
    }
    /**
     * 获取偏远地区增加邮费
     */
    public function getRemoteAreasPrice()
    {
        $res = Db::name('config')->where('name',"yuan_postage")->where('group',"postage")->value("value");
        // $res = json_decode($res,true);
        return $res;
    }
}